/**
 * String Utils.
 *
 * @export
 * @class StringUtils
 */
export class StringUtils {
    /**
     * check if string variable is blank.
     *
     * @static
     * @param {string} input
     * @returns {Boolean}
     *
     * @memberOf StringUtils
     */
    public static isBlank(input: string): boolean {
        // tslint:disable-next-line:triple-equals
        return (input == null) || (typeof input === 'string' && input.trim().length <= 0);
    }
    /**
     * check if string vairable is not blank.
     *
     * @static
     * @param {string} input
     * @returns {Boolean}
     *
     * @memberOf StringUtils
     */
    public static isNotBlank(input: string): boolean {
        return !StringUtils.isBlank(input);
    }
    /**
     * format string variable via index. {0},{1}..
     *
     * @static
     * @param {string} input
     * @param {...string[]} paras
     * @returns {string}
     *
     * @memberOf StringUtils
     */
    public static format(input: string, ...paras: string[]): string {
        // tslint:disable-next-line:triple-equals
        if (input == null
            || (typeof input !== 'string' || input.trim().length <= 0)
            // tslint:disable-next-line:triple-equals
            || paras == null
            || paras.length <= 0) {
            return input;
        }

        // var str = paras[0];
        for (let i = 0; i < paras.length; i++) {
            const re = new RegExp('\\{' + (i) + '\\}', 'gm');
            input = input.replace(re, paras[i] + '');
        }
        return input;
    }

    /**
     * format string variable via object's key.
     * ex: paras.a = 1 leads to "test {a}" => "test 1"
     *
     * @static
     * @param {string} input
     * @param {*} paras
     * @returns {string}
     *
     * @memberOf StringUtils
     */
    public static formatObj(input: string, paras: any): string {
        // tslint:disable-next-line:triple-equals
        if (input == null
            || (typeof input !== 'string' || input.trim().length <= 0)
            // tslint:disable-next-line:triple-equals
            || paras == null) {
            return input;
        }

        for (const key of Object.keys(paras)) {
            // tslint:disable-next-line:triple-equals
            if (paras.hasOwnProperty(key) && paras[key] != null) {
                const re = new RegExp('\\{' + (key) + '\\}', 'gm');
                input = input.replace(re,  paras[key] + '');
            }
        }
        return input;
    }
}
